package com.jshx.sz.gdjt.thread;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import com.jshx.sz.gdjt.util.AccessDBImp;
import com.jshx.sz.gdjt.util.TDESEncodeUtils;
import com.jshx.sz.gdjt.constant.ApplicationGlobal;

import android.content.pm.PackageInfo;
import android.os.Handler;
import android.os.Message;
 

public class GetTaskThread  extends Thread{
	private Handler handler;
	private String params;
	private ArrayList  returnList = new ArrayList ();
	
	public GetTaskThread()
	{
		
	}
	public GetTaskThread(Handler handler, String params, ArrayList  data)
	{
		this.handler=handler;
		this.params=params;
		this.returnList=data;
	}
	public void run() 
	{
		try {
			AccessDBImp dao= new AccessDBImp();
			String re=dao.proDB("getTaskList", params);
			ArrayList reList=new ArrayList();
			reList=dao.splitDataList(re);
			proChild(  reList  );
			//dao.dataCopyArrayListToArrayList(reList, returnList);
			Message msg=new Message();
			msg.what=ApplicationGlobal.data_obtained;
			handler.sendMessage(msg) ;
		} catch (Exception e) {
			System.out.println(e);
			e.printStackTrace();
			Message msg=new Message();
			msg.what=ApplicationGlobal.data_missed;
			handler.sendMessage(msg) ;
		}  
	}
	
	private void proChild(ArrayList  reList  )
	{
		HashMap<String, ArrayList> childMap=new HashMap<String, ArrayList>();
		if(reList!=null&&reList.size()>0)
		{
			for(int i=0;i<reList.size();i++)
			{
				HashMap map=(HashMap)reList.get(i);
				String dataType=map.get("dataType")+"";
				String order1=map.get("order1")+"";
				if(dataType!=null&&dataType.equals("1"))
				{
					returnList.add(map);
				}
				else if(dataType!=null&&dataType.equals("2"))
				{
					ArrayList temp=childMap.get(order1);
					if(temp==null)
					{
						temp=new ArrayList();
					}	
					temp.add(map);
					childMap.put(order1, temp);
				}
			}
			
			for(int i=0;i<returnList.size();i++)
			{
				HashMap map=(HashMap)returnList.get(i);
				String order1=map.get("order1")+"";
				map.put("child", childMap.get(order1));
			}
		}
	}
	
	 
}
